package com.trailer.base.base

import android.os.IBinder
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.trailer.base.R
import com.trailer.base.net.IResp
import com.trailer.base.view.BRView

abstract class BRActivity<VM : BRViewModel<*>, A : BaseQuickAdapter<*, out BaseViewHolder>, B : ViewDataBinding> : BActivity<VM, B>(), BRView<A> {
    open lateinit var mAdapter: A

    protected var recyclerView: RecyclerView? = null

    private var refresh_layout: SwipeRefreshLayout? = null
    override fun initView(view: View?) {
        super.initView(view)
        recyclerView = findViewById(R.id.recycler_view)
        refresh_layout = findViewById(R.id.refresh_layout)
        mAdapter = initAdapter()
        recyclerView?.adapter = mAdapter
        //        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
    }

    override fun listeners() {
        super.listeners()
        /*设置Item点击事件*/
//            mAdapter?.setOnItemClickListener(this)
//            mAdapter?.setOnItemChildClickListener(this)
        mAdapter.loadMoreModule.isEnableLoadMore = needLoadMore()
        if (needLoadMore()) {
            /*上拉加载设置*/
            mAdapter.loadMoreModule.setOnLoadMoreListener { loadMore() }
        }
        /*列表预加载*/
//            adapter.setPreLoadNumber(5);

        /*下拉刷新设置*/if (refresh_layout != null) {
            refresh_layout!!.setColorSchemeResources(R.color.refresh_colorPrimary, R.color.refresh_colorAccent, R.color.refresh_colorPrimaryDark)
            refresh_layout!!.setOnRefreshListener { onRefresh() }
            refresh_layout!!.isEnabled = isNeedRefresh
        }
    }


    override fun refreshing(refreshing: Boolean) {
        if (refresh_layout != null) {
            refresh_layout!!.isRefreshing = refreshing
        }
    }

    override fun vmClass(): Class<VM> {
        return BRViewModel::class.java as Class<VM>
    }

    override fun needLoadMore(): Boolean {
        return true
    }

    override val isNeedRefresh: Boolean
        get() = true


    override fun loadMore() {
//        vm.repository().reqList().subscribe()
        vm.reqMore()
    }


    override fun getTotal(total: Int) {}

    //点击其他地方键盘收回
    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        if (ev.action == MotionEvent.ACTION_DOWN) {
            val v = currentFocus
            if (isShouldHideKeyboard(v, ev)) {
                hideKeyboard(v!!.windowToken)
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    /**
     * 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘，因为当用户点击EditText时则不能隐藏
     *
     * @param v
     * @param event
     *
     * @return
     */
    private fun isShouldHideKeyboard(v: View?, event: MotionEvent): Boolean {
        if (v != null && v is EditText) {
            val l = intArrayOf(0, 0)
            v.getLocationInWindow(l)
            val left = l[0]
            val top = l[1]
            val bottom = top + v.getHeight()
            val right = left + v.getWidth()
            return !(event.x > left && event.x < right && event.y > top && event.y < bottom)
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上，和用户用轨迹球选择其他的焦点
        return false
    }

    /**
     * 获取InputMethodManager，隐藏软键盘
     *
     * @param token
     */
    private fun hideKeyboard(token: IBinder?) {
        if (token != null) {
            val im = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS)
        }
    }

    override fun onListReceive(res: IResp<*>) {}

    override fun adapter(): A = mAdapter

}
